package ui.fragment.homepagefragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.CourseraAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.CourseraBean;
import student.andlei.com.student.R;
import ui.activity.ClassTimeTableActivity;
import ui.activity.CourseraInfoActivity;
import utils.GsonUtils;

/**
 * 课程
 */
public class CourseraFragment extends BaseNetFragment {
    private RadioGroup radioGroup;
    private RadioButton rbt_all,
            rbt_schoolclass,
            rbt_extracurricularclass;
    private RecyclerView recyclerView;
    private List<CourseraBean> list = new ArrayList<>();
    private CourseraAdapter adapter;
    private int type;
    public static Fragment getInstance() {
        return new CourseraFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (baseBean.code == 200) {
            list = GsonUtils.getInstance(getActivity()).parseNoHeaderJArray(baseBean,CourseraBean.class);
            if (list != null ) {
                adapter.setNewData(list);
            }else {
                list = new ArrayList<>();
                adapter.setNewData(list);
            }
//            //拿到本地JSON 并转成String
//            String strByJson = baseBean.data.toString();
//            //Json的解析类对象
//            JsonParser parser = new JsonParser();
//            //将JSON的String 转成一个JsonArray对象
//            JsonArray jsonArray;
//            try {
//                jsonArray = parser.parse(strByJson).getAsJsonArray();
//            } catch (Exception e) {
//                toast("" + baseBean.msg);
//                return;
//            }
//            Gson gson = new Gson();
//            //加强for循环遍历JsonArray
//            for (JsonElement user : jsonArray) {
//                //使用GSON，直接转成Bean对象
//                CourseraBean Bean = gson.fromJson(user, CourseraBean.class);
//                list.add(Bean);
//            }

        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_course;
    }

    @Override
    protected void findViews() {
        radioGroup = getView(R.id.rd_group);
        rbt_all = getView(R.id.rb_all);
        rbt_schoolclass = getView(R.id.rb_schoolclass);
        rbt_extracurricularclass = getView(R.id.rb_extracurricularclass);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        recyclerView.setLayoutManager(new GridLayoutManager(this.getActivity(), 3));
        adapter = new CourseraAdapter(context, R.layout.item_coursera, list);
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEmptyView(R.layout.empty_view);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    //全部
                    case R.id.rb_all:
                        type = 0;
                        loaddata(0);
                        break;
                    //校内班级
                    case R.id.rb_schoolclass:
                        type = 1;
                        loaddata(1);
                        break;
                    //课外班级
                    case R.id.rb_extracurricularclass:
                        type = 2;
                        loaddata(2);
                        break;
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                //1:校内课程 2:校外课程
                if(list.get(position).schedule_type == 1){
                    bundle.putInt("schedule_id",list.get(position).schedule_id);
                    jumpTo(ClassTimeTableActivity.class, bundle);
                }else {
                    bundle.putInt("schedule_id",list.get(position).schedule_id);
                    jumpTo(CourseraInfoActivity.class, bundle);
                }

            }
        });
    }

    @Override
    protected void formatData() {
//        list.add(new CourseraBean());
//        list.add(new CourseraBean());
//        list.add(new CourseraBean());
        loaddata(0);
    }

    private void loaddata(int type) {
        //0:全部[默认] 1:校内课程 2:校外课程
        Map<String, String> map = new HashMap<>();
        map.put("schedule_type", ""+type);
        post("schedule/scheduleList", map, CourseraBean.class, false);
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loaddata(type);
        }
    }
}
